package sdfi.lee.JMS;

import javax.jms.Connection;

import org.apache.activemq.ActiveMQConnectionFactory;

import sdfi.lee.Properties.ReaderProperties;

public class ConnectionFactory {
	private static Connection con=null;
	private static ActiveMQConnectionFactory factory=null;
	static
	{
			try
			{
				ReaderProperties reader=new ReaderProperties();
				String port=reader.getProperty("mq_port");
				String path=reader.getProperty("mq_path");
			
				String url="tcp://192.168.0.112:61616";
				//String url="";
				if(path!=null&&port!=null&&!"".equals(path)&&!"".equals(port))
				{
					
				}
				else
				{
					port="61616";
					path="localhost";
				}
				url="tcp://"+path+":"+port;
				System.out.println("url:"+url);
		    	factory=new ActiveMQConnectionFactory(url);
				con=factory.createConnection();
			}
			catch(Exception ee)
			{
				ee.printStackTrace();
			}
			
	}
	
	public static Connection getCon() {
		try
		{	
		
			if(null==con) 
			{
				if(factory==null)
				{
					ReaderProperties rp=new ReaderProperties();
					String port=rp.getProperty("mq-port");
					String path=rp.getProperty("mq-path");
					String url="tcp://localhost:61616";
					if(path!=null&&port!=null&&!"".equals(path)&&!"".equals(port))
						url=path+":"+port;

			    	factory=new ActiveMQConnectionFactory(url);
				}
				con=factory.createConnection();
			}
		
				
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
		}
		return con;
	}
/*
	public static Connection getCon()
	{
		Connection con=null;
		try
		{
			if(factory==null)
			{
				ReaderProperties rp=new ReaderProperties();
				String port=rp.getProperty("mq-port");
				String path=rp.getProperty("mq-path");
				String url="tcp://192.168.0.112:61616";
				if(path!=null&&port!=null&&!"".equals(path)&&!"".equals(port))
					url=path+":"+port;

		    	factory=new ActiveMQConnectionFactory(url);
			}
			con=factory.createConnection();
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
		}
		return con;
		
	}
		*/

	public static void setCon(Connection con) {
		ConnectionFactory.con = con;
	}
	public void stopCon()
	{
		try
		{
			con.stop();
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
		}
	}
	public static void startCon()
	{
		try
		{
			con.start();
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
		}
	}
	public static void closeCon()
	{
		try
		{
			con.close();
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
		}
	}
}
